package com.key.activity;

import java.util.Iterator;
import java.util.Set;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import com.key.util.InfoText;
import com.key.util.Util;
import com.key.wanderbupt.R;

public class KeySearchActivity extends Activity implements OnClickListener {
	
	public ProgressDialog pDialog = null;
	
	private ImageButton searchButton = null;
	private EditText searchEdit = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.search);
		
		initEditText();
		initSearchButton();
	}
	
	private void initEditText() {
		searchEdit = (EditText) findViewById(R.id.searchEditText);
	}
	
	private void initSearchButton() {
		
		searchButton = (ImageButton) findViewById(R.id.searchButton);
		searchButton.setOnClickListener(this);
		searchButton.setOnTouchListener(new View.OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					((ImageButton) v).setBackgroundDrawable(getResources()
							.getDrawable(R.drawable.search));
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					((ImageButton) v).setBackgroundDrawable(getResources()
							.getDrawable(R.drawable.search));
				}
				return false;
			}
		});
	}
	
	private boolean Search() {
		DialogStart();
		return checkInput();
	}
	
	private void DialogStart() {
		
		if( pDialog != null ) {
			pDialog.cancel();
		}
		pDialog = new ProgressDialog(KeySearchActivity.this);
		pDialog.setTitle("提示");
		pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		pDialog.setMessage("正在搜索中,请稍后...");
		pDialog.setIndeterminate(true);
		pDialog.show();
	}
	
	private void DialogEnd() {
		
		if (pDialog != null) {
			pDialog.cancel();
		}
	}
	
	private boolean checkInput() {
		
		String searchText = searchEdit.getText().toString();
		
		if( searchText.length() == 0 ) {
			DialogEnd();
			Util.toastPopWindow(this,"请填写要搜索的内容");
			return false;
		}
		
		Set<String> result = InfoText.searchBuilding(searchText);
		if( result.size() == 0 ) {
			DialogEnd();
			Util.toastPopWindow(this,"没有您要的搜索结果");
			return false;
		}
		
		int pos = 0;
		Util.result = new String[result.size()];
		Iterator<String> iter = result.iterator();
		while( iter.hasNext() ) {
			Util.result[pos++] = iter.next();
		}
		
		return true;
	}

	@Override
	public void onClick(View v) {
		Intent gameIntent = null;
		switch (v.getId()) {
		case R.id.searchButton:
			if( Search() == false ) {
				break;
			}
			gameIntent = new Intent(this,KeySearchDataActivity.class);
			this.startActivity(gameIntent);
			this.finish();
			break;
		default:
			break;
		}
	}
}


